<?php
class m111006_110021_add_deal_section extends CDbMigration
{
	public function safeUp()
	{
		/** New table dycrm_deal **/
		$this->createTable('dycrm_deal', array(
			'id' => 'pk',
  			'teamId' => 'integer NULL',
  			'createUserId' => 'integer NULL',
  			'referenceNo' => 'string NOT NULL',
  			'bank' => 'string NOT NULL',
		));
		$this->addForeignKey('deal_fk_team', 
			'dycrm_deal', 'teamId', 'dycrm_team', 'id','SET NULL','CASCADE');
		$this->addForeignKey('deal_fk_create_user', 
			'dycrm_deal', 'createUserId', 'dycrm_user', 'id','SET NULL','CASCADE');
		$this->createIndex('deal_index_reference_no', 'dycrm_deal', 'referenceNo');
		
		
		/** Create table dycrm_deal_customer **/
		$this->createTable('dycrm_deal_customer', array(
			'id' => 'pk',
  			'dealId' => 'integer NOT NULL',
  			'customerId' => 'integer NOT NULL',
		));
		$this->addForeignKey('deal_customer_fk_deal', 
			'dycrm_deal_customer', 'dealId', 'dycrm_deal', 'id','CASCADE','CASCADE');
		$this->addForeignKey('deal_customer_fk_customer', 
			'dycrm_deal_customer', 'customerId', 'dycrm_customer', 'id','CASCADE','CASCADE');
		
		
		/** Add new team reception **/
		$this->insert('dycrm_team', array('id'=>100, 'name'=>'Reception'));
		
		
		/** Alter globel view in table dycrm_user **/
		$this->alterColumn('dycrm_user', 'globalView', 'boolean NULL DEFAULT 0');
		
		/** Alter table dycrm_loan **/
		$this->alterColumn('dycrm_loan', 'bank', 'string NULL');
		$this->alterColumn('dycrm_loan', 'customerId', 'integer NULL');
		$this->addColumn('dycrm_loan', 'dealId', 'integer');
		$this->addForeignKey('loan_fk_deal', 
			'dycrm_loan', 'dealId', 'dycrm_deal', 'id','CASCADE','CASCADE');
		$this->dropForeignKey('dycrm_loan_ibfk_1', 'dycrm_loan');
		
		
		/** Alter table dycrm_attachment**/
		$this->alterColumn('dycrm_attachment', 'customerId', 'integer NULL');
		$this->addColumn('dycrm_attachment', 'dealId', 'integer');
		$this->addForeignKey('attachment_fk_deal', 
			'dycrm_attachment', 'dealId', 'dycrm_deal', 'id','CASCADE','CASCADE');
		$this->dropForeignKey('dycrm_attachment_ibfk_1', 'dycrm_attachment');
		
		/** Add auth item **/
		$this->insert('dycrm_auth_item', array('name'=>'dealInfoAccess','type'=>1, 'bizrule'=>'return User::checkDealInfoAccess($params["dealId"]);'));
		
		/** Add auth item child **/
		$this->insert('dycrm_auth_item_child', array('parent'=>'staff','child'=>'dealInfoAccess'));
	}

	public function safeDown()
	{
		/** delete table dycrm_deal **/
		$this->dropTable('dycrm_deal');
		
		
		/** delete table dycrm_deal_customer **/
		$this->dropTable('dycrm_deal_customer');
		
		
		/** Delete Team reception **/
		$this->delete('dycrm_team','id = 100');
		
		
		/** Revert change on dycrm_loan **/
		$this->dropColumn('dycrm_loan', 'dealId');
		$this->dropForeignKey('loan_fk_deal', 'dycrm_loan');
		$this->addForeignKey('dycrm_loan_ibfk_1', 
			'dycrm_loan', 'customerId', 'dycrm_customer', 'id','CASCADE','CASCADE');
	
		/** Revert change on dycrm_attachment **/
		$this->dropColumn('dycrm_attachment', 'dealId');
		$this->dropForeignKey('attachment_fk_deal', 'dycrm_attachment');
		$this->addForeignKey('dycrm_attachment_ibfk_1', 
			'dycrm_attachment', 'customerId', 'dycrm_customer', 'id','CASCADE','CASCADE');
		
		/** Revert change on auth item **/
		$this->delete('dycrm_auth_item', 'name = "dealInfoAccess"');
		
		/** Revert change on auth item child **/
		$this->delete('dycrm_auth_item_child', 'child = "dealInfoAccess"');
	}
}